home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.09 Sep 93 / FrameAnim App ƒ / Events.c < prev    next >
Encoding:
Text File  |  1994-11-06  |  6.2 KB  |  261 lines  |  [TEXT/KAHL]

  1. // ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  2. // • Program:    FrameAnim
  3. // • File:        Events.c
  4. // •
  5. // • Copyright © 1993 by Scott B. Steinman, O.D., Ph.D. All Rights Reserved.
  6. // ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  7.  
  8. #include "FrameAnim.h"
  9.  
  10. // • ------------------ External Globals ---------------------------------- 
  11.  
  12. extern CWindowPtr        gMainWindow;    // • From Main.c file    
  13. extern EventRecord        gEvent;            // • From Main.c file
  14. extern Flags            gFlags;            // • From Main.c file
  15.  
  16. // • ------------------ Static Functions ---------------------------------- 
  17.  
  18. static void        ActivateEvent( void );
  19. static void        HandleAppleMenu( const short );
  20. static void        HandleFileMenu( const short );
  21. static void        HandleAnimMenu( const short );
  22. static void        MouseEvent( void );
  23. static void        ProcessMenus( const long );
  24. static void        UpDateEvent( void );
  25.  
  26. // • ------------------ Handle Menu Selections ---------------------------- 
  27.  
  28. static void
  29. ProcessMenus( const long menuChoice )
  30. // •
  31. // • Control center for menus.
  32. {
  33.     short        menuNo, itemNo;
  34.     
  35.     
  36.     if (menuChoice != 0) {
  37.         menuNo = HiWord( menuChoice );            // • Passed from Mouse Handler. 
  38.         itemNo = LoWord( menuChoice );
  39.         switch( menuNo ) {
  40.             case kAppleID:     HandleAppleMenu( itemNo );        break;
  41.             case kFileID:     HandleFileMenu( itemNo );        break;
  42.             case kAnimID:     HandleAnimMenu( itemNo );        break;
  43.         }
  44.         ResetWindBkgnd();
  45.         HiliteMenu( 0 );                        // • Turn off menu highlighting. 
  46.     }
  47. }
  48.  
  49. // • ------------------ Handle Apple Menu Selections ---------------------- 
  50.  
  51. static void
  52. HandleAppleMenu( const short itemNo )
  53. // •
  54. // • Control center for Apple menu.
  55. {
  56.     short    bogus, deskAcc;
  57.     Str255    nameHolder;                    // • Holds the name of desired DA 
  58.  
  59.  
  60.     switch( itemNo ) {
  61.         case kAboutAnimItem: 
  62.             bogus = Alert( kAboutAlertID, NullPointer );    
  63.             break;
  64.         default:                        // • This makes the desk accesories available. 
  65.             GetItem( GetMHandle( kAppleID ), itemNo, &nameHolder );
  66.             deskAcc = OpenDeskAcc( &nameHolder );
  67.             break;
  68.     }
  69. }
  70.  
  71. // • ------------------ Handle File Menu Selections ----------------------- 
  72.  
  73. static void
  74. HandleFileMenu( const short itemNo )
  75. // •
  76. // • This is the control center for the File menu.
  77. {
  78.     switch( itemNo ) {
  79.         case kOpenFilmItem:
  80.             OpenFilm();
  81.             if (gFlags.cancel == false) {
  82.                 EnableItem( GetMHandle( kAnimID ), kPlayItem );
  83.                 EnableItem( GetMHandle( kFileID ), kSaveFilmItem );
  84.             }
  85.             break;            
  86.         case kSaveFilmItem:
  87.             SaveFilm();
  88.             break;
  89.         // • ------- Blank Line 
  90.         case kQuitItem: 
  91.             gFlags.done = true;                
  92.             break;
  93.     }
  94. }
  95.  
  96. // • ------------------ Handle Animation Menu Selections ------------------ 
  97.  
  98. static void
  99. HandleAnimMenu( const short itemNo )
  100. // •
  101. // • Control center for Option menu.
  102. {
  103.     switch( itemNo ) {
  104.         case kPlayItem:
  105.             PlayFilm();
  106.             break;
  107.         // • --------- Blank Line  
  108.         case kFrameSzItem: 
  109.             FrameSzDlg();
  110.             break;
  111.         case kGrayItem: 
  112.             GrayDlg();
  113.             break;
  114.         // • --------- Blank Line  
  115.         case kFilmItem: 
  116.             FilmDlg();
  117.             if (gFlags.cancel == false) {
  118.                 DoFilm();
  119.                 EnableItem( GetMHandle( kAnimID ), kPlayItem );
  120.                 EnableItem( GetMHandle( kFileID ), kSaveFilmItem );
  121.             }
  122.             gFlags.cancel = false;     // • Reset cancel status to false for next time 
  123.             break;
  124.     }
  125. }
  126.  
  127. // • ------------------ Mouse Event Handler ------------------------------- 
  128.  
  129. static void
  130. MouseEvent( void )
  131. // •
  132. // • Keep track of mouse activity.
  133. {
  134.     WindowPtr        windowPointedTo = NullPointer;
  135.     Point            mouseLoc;
  136.     long            menuChoice;
  137.     short            windowPart, location;
  138.     
  139.     
  140.     mouseLoc = gEvent.where;                        
  141.     windowPart = FindWindow( mouseLoc, &windowPointedTo );
  142.     switch( windowPart ) {                
  143.         case inMenuBar: 
  144.             menuChoice = MenuSelect( mouseLoc );
  145.             ProcessMenus( menuChoice );
  146.             break;
  147.         case inSysWindow:                         
  148.             SystemClick( &gEvent, windowPointedTo );
  149.             break;
  150.         case inContent:                         
  151.             if (windowPointedTo != FrontWindow())
  152.                 SelectWindow( windowPointedTo );
  153.             else {
  154.                 SetPort( windowPointedTo );
  155.                 PmForeColor( kPalBkgnd );
  156.                 PaintRect( &(windowPointedTo->portRect) );    
  157.                 ResetForeAndBackColors();
  158.             }
  159.             break;
  160.     }
  161. }
  162.  
  163. // • ------------------ Key Down Event Handler ---------------------------- 
  164.  
  165. static void
  166. KeyEvent( void )
  167. // •
  168. // • Controls the input through keyboard. 
  169. {
  170.     char        charCode;
  171.     
  172.     charCode = gEvent.message & charCodeMask;
  173.     if (BitAnd( gEvent.modifiers, cmdKey ) != 0)    // • Command Key Pressed 
  174.         ProcessMenus( MenuKey( charCode ) );        // • Process "/?" combinations. 
  175. }
  176.  
  177. // • ------------------ Activate Event Handler ---------------------------- 
  178.  
  179. static void
  180. ActivateEvent( void )
  181. // •
  182. // • This is Generic "make things happen" procedure.
  183. {
  184.     WindowPtr        targetWindow;
  185.     
  186.     
  187.     targetWindow = (WindowPtr) gEvent.message;
  188.     if (gEvent.modifiers & 1) {                    // • Window needs to be activated 
  189.         SetPort( targetWindow );
  190.         ResetWindBkgnd();                    
  191.     }
  192.     else {
  193.         ;                                        // • Deactivate window or whatever 
  194.     }
  195. }
  196.  
  197. // • ------------------ Update Event Handler ------------------------------ 
  198.  
  199. static void
  200. UpDateEvent( void )
  201. // •
  202. // • Handles update events.
  203. {
  204.     GDHandle    currentDev = NullHandle;
  205.     CGrafPtr    currentPort = NullPointer,
  206.                 updateWindow;
  207.     
  208.     GetGWorld( ¤tPort, ¤tDev );        // • Save current port & device  
  209.         
  210.     updateWindow = (CGrafPtr) gEvent.message;
  211.     SetPort( updateWindow );
  212.     BeginUpdate( updateWindow );
  213.     ResetWindBkgnd();
  214.     EndUpdate( updateWindow );
  215.     
  216.     SetGWorld( currentPort, currentDev );        // • Reset port & device  
  217. }
  218.  
  219. // • ------------------ Reset Window Background Color --------------------- 
  220.  
  221. void
  222. ResetWindBkgnd( void )
  223. // •
  224. // • The window background color must sometimes be reset 
  225. // • after a dialog box presentation.
  226. {
  227.     PmBackColor( kPalBkgnd );
  228.     EraseRect( &(gMainWindow->portRect) );    // • Clear the area completely 
  229.     ResetForeAndBackColors();
  230. }
  231.  
  232. // • ------------------ Main Event Loop ----------------------------------- 
  233.  
  234. void
  235. MainEventLoop( void )
  236. // •
  237. // • Event dispatch routine.
  238. {
  239.     do {
  240.         WaitNextEvent( everyEvent, &gEvent, kMinSleep, kNilMouseRegion );
  241.         switch( gEvent.what ) {
  242.             case keyDown:                         
  243.                 KeyEvent();
  244.                 break;
  245.             case activateEvt:                     
  246.                 ActivateEvent();
  247.                 break;
  248.             case updateEvt:                         
  249.                 UpDateEvent();
  250.                 break;
  251.             case mouseDown:                     
  252.                 MouseEvent();
  253.                 break;
  254.             case nullEvent:
  255.             default:
  256.                 break;
  257.         }
  258.     } while ( !    gFlags.done );            // • Until quit is chosen. 
  259. }
  260.  
  261.